<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../plugins/element-ui/index.css" />
    <link rel="stylesheet" href="../../styles/common.css" />
    <link rel="stylesheet" href="../../styles/page.css" />
</head>
<body>
<div class="addBrand-container" id="exam-add-app">
    <div class="container">
        <el-form
                ref="ruleForm"
                :model="ruleForm"
                :rules="rules"
                :inline="false"
                label-width="180px"
                class="demo-ruleForm"
        >
            <el-form-item label="考试名称:" prop="examname">
                <el-input v-model="ruleForm.examname" placeholder="考试名称" maxlength="20"/>
            </el-form-item>
            <el-form-item label="考试类型:" prop="type">
                <el-input v-model="ruleForm.type" placeholder="请输入考试类型" maxlength="20"/>
            </el-form-item>
            <el-form-item label="开始日期:" prop="startTime">
                <el-date-picker v-model="ruleForm.startTime" value-format="yyyy-MM-dd" type="date" placeholder="请选择开始日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="结束日期:" prop="endTime">
                <el-date-picker v-model="ruleForm.endTime" value-format="yyyy-MM-dd" type="date" placeholder="请选择结束日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="考试总分:" prop="score">
                <el-input v-model="ruleForm.score" placeholder="请输入考试总分" maxlength="20"/>
            </el-form-item>
            <el-form-item label='考场:' prop='place'>
                <el-select v-model='ruleForm.place' placeholder='考场选择'>
                    <el-option label='A401' :value='1'></el-option>
                    <el-option label='A402' :value='2'></el-option>
                    <el-option label='A403' :value='3'></el-option>
                    <el-option label='A404' :value='4'></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="及格分:" prop="pass">
                <el-input v-model="ruleForm.pass" placeholder="请输入及格分" maxlength="20"/>
            </el-form-item>
            <el-form-item label="人数:" prop="number">
                <el-input v-model="ruleForm.number" placeholder="请输入总人数" maxlength="20"/>
            </el-form-item>
            <div class="subBox address">
                <el-form-item>
                    <el-button  @click="goBack()">
                        取消
                    </el-button>
                    <el-button
                            type="primary"
                            @click="submitForm('ruleForm', false)"
                    >
                        保存
                    </el-button>
                    <el-button
                            v-if="actionType == 'add'"
                            type="primary"
                            class="continue"
                            @click="submitForm('ruleForm', true)"
                    >
                        保存并继续添加
                    </el-button>
                </el-form-item>
            </div>
        </el-form>
    </div>
</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../js/validate.js"></script>
<script src="../../js/index.js"></script>
<script src="../../api/exam.js"></script>
<script>
    new Vue({
        el: '#exam-add-app',
        data() {
            return {
                id: '',
                actionType : '',
                ruleForm : {
                    'examname': '',
                    'type': '',
                    'startTime': '',
                    'endTime': '',
                    'score':'',
                    'place':'',
                    'pass':'',
                    'number':'',
                }
            }
        },
        computed: {
            rules () {
                return {
                    examname: [{required: true, trigger: 'blur'}],
                    type: [{ required: true, 'trigger': 'blur' }],
                    startTime: [{ 'required': true, 'trigger': 'blur' }],
                    endTime: [{ 'required': true, 'trigger': 'blur' }],
                    score: [{ 'required': true, 'trigger': 'blur' }],
                    place: [{ 'required': true, 'trigger': 'blur' }],
                    pass: [{ 'required': true, 'trigger': 'blur' }],
                    number: [{ 'required': true, 'trigger': 'blur' }]
                }
            }
        },
        created() {
            this.id = requestUrlParam('id')
            this.actionType = this.id ? 'edit' : 'add'
            if (this.id) {
                this.init()
            }
        },
        mounted() {
        },
        methods: {
            async init () {
                queryExamById(this.id).then(res => {
                    // console.log("账户.....：",res)
                    if (String(res.code) === '1') {
                        console.log(res.data)
                        this.ruleForm = res.data
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                })
            },
            submitForm (formName, st) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if (this.actionType === 'add') {
                            const params = {
                                ...this.ruleForm
                            }
                            addExam(params).then(res => {
                                if (res.code === 1) {
                                    this.$message.success('考试添加成功！')
                                    if (!st) {
                                        this.goBack()
                                    } else {
                                        this.ruleForm = {
                                            'examname': '',
                                            'type': '',
                                            'startTime': '',
                                            'endTime': '',
                                            'score':'',
                                            'place':'',
                                            'pass':'',
                                            'number':'',
                                        }
                                    }
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })
                        } else {
                            const params = {
                                ...this.ruleForm,
                            }
                            editExam(params).then(res => {
                                if (res.code === 1) {
                                    this.$message.success('考生信息修改成功！')
                                    this.goBack()
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })
                        }
                    } else {
                        console.log('error submit!!')
                        return false
                    }
                })
            },
            goBack(){
                window.parent.menuHandle({
                    id: '3',
                    url: '/backend/page/exam/list.html',
                    name: '考试管理'
                },false)
            }
        }
    })
</script>
</body>
</html>